layui.use(['table', 'admin', 'ax', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    /**
     * 运营部表（原系统：分公司表）管理
     */
    var BasicOperate = {
        tableId: "basicOperateTable"
    };

    /**
     * 初始化表格的列
     */
    BasicOperate.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {align: 'center', toolbar: '#tableBar', title: '操作', width:120},
            {field: 'status', sort: true, title: '状态', templet: '#statusTpl'},
            {field: 'coName', title: '核算主体'},
            {field: 'operateName', title: '名称', width:150},
            {field: 'abbreviation', title: '简称'},
            {field: 'operateContacts', title: '联系人'},
            {field: 'operateTel', title: '电话'},
            {field: 'operateEmall', title: '邮箱', hide: true},
            {field: 'operateProvinces', title: '省份', hide: true},
            {field: 'operateCiywhere', title: '城市', hide: true},
            {field: 'operateBank', title: '开户行', hide: true},
            {field: 'operateBankaccount', title: '银行账号', hide: true},
            {field: 'operateCreditcode', title: '社会统一信用代码', hide: true},
            {field: 'operateAddress', title: '地址', hide: true},
            {field: 'operateNotes', title: '备注'},
            {field: 'createTime', title: '创建时间'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicOperate.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(BasicOperate.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    BasicOperate.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/basicOperate/add';
    };

    /**
     * 导出excel按钮
     */
    BasicOperate.exportExcel = function () {
        var checkRows = table.checkStatus(BasicOperate.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    BasicOperate.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/basicOperate/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BasicOperate.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/basicOperate/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BasicOperate.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };



    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicOperate.tableId,
        url: Feng.ctxPath + '/basicOperate/list',
        page: true,
        limit: 40,
        height: "full-60",
        cellMinWidth: 100,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        cols: BasicOperate.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicOperate.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        BasicOperate.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BasicOperate.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BasicOperate.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BasicOperate.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BasicOperate.onDeleteItem(data);
        }
    });

    // 修改参数状态
    form.on('switch(status)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        BasicOperate.changeGradingstatus(id, checked);
    });

    /**
     * 修改参数状态
     *
     * @param id 参数id
     * @param checked 是否选中（true,false），选中就是启用用户，未选中就是禁用用户
     */
    BasicOperate.changeGradingstatus = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/basicOperate/editItem", function (data) {
                Feng.success("启用成功!");
            }, function (data) {
                Feng.error("启用失败!");
                table.reload(BasicOperate.tableId);
            });
            ajax.set("status", 1);
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/basicOperate/editItem", function (data) {
                Feng.success("禁用成功!");
            }, function (data) {
                Feng.error("禁用失败!" + data.responseJSON.message + "!");
                table.reload(BasicOperate.tableId);
            });
            ajax.set("status", 2);
            ajax.set("id", id);
            ajax.start();
        }
    };
});
